/* AnnouncementService.java
 *
 * Copyright (C) 2013 Universidad de Sevilla
 * 
 * The use of this project is hereby constrained to the conditions of the 
 * TDG Licence, a copy of which you may download from 
 * http://www.tdg-seville.info/License.html
 * 
 */

package services;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import repositories.AnnouncementRepository;

import domain.Announcement;
import domain.Customer;

@Service
@Transactional
public class AnnouncementService {

	// Managed repository -----------------------------------------------------
	@Autowired 
	AnnouncementRepository announcementRepository;
	// TODO: Add managed repository.	
	
	// Supporting services ----------------------------------------------------
	@Autowired
	RegistrationService regitrationService;
	@Autowired
	CustomerService customerService;
	// TODO: Add supporting services.
	
	// Constructors -----------------------------------------------------------

	public AnnouncementService() {
		super();
	}
	
	// Simple CRUD methods ----------------------------------------------------
	
	public Announcement create() {
		Announcement result;

		// TODO: Complete this method.
		
		result = new Announcement();
		result.setMoment(new Date());

		return result;
	}
	
	public Collection<Announcement> findAll() {
		Collection<Announcement> result;

		// TODO: Complete this method.
		result = announcementRepository.findAll();
				
		return result;
	}

	public Announcement findOne(int announcementId) {
		Announcement result;
		
		// TODO: Complete this method.
		result = announcementRepository.findOne(announcementId);
		
		return result;
	}
	
	public void save(Announcement announcement) {
		// TODO: Complete this method.
		assert announcement != null;
		announcement.setMoment(new Date());
		announcementRepository.save(announcement);
	}	
	
	public void delete(Announcement announcement) {
		// TODO: Complete this method.
		assert announcement != null;
		announcementRepository.delete(announcement);
	}

	// Other business methods -------------------------------------------------
	
	public Collection<Announcement> findRegistered() {
		Collection<Announcement> result = new HashSet<Announcement>();

		// TODO: Complete this method.
		Customer customer  = customerService.findByPrincipal();
		result = announcementRepository.findAllRegister(customer);
		return result;
	}

	public Collection<Announcement> findAllActive() {
		Collection<Announcement> result;

		// TODO: Complete this method.
		result = announcementRepository.findAllActive(new Date());
		
		return result;
	}
	
}